package com.kongfuzi.student.ui.global;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.kongfuzi.student.R;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ZhilianbaoActivity extends CustomerActionBarActivity {

    @Bind(R.id.gv)
    GridView mGv;
    private int[] imgs = {R.drawable.dianshi, R.drawable.kongtiao, R.drawable.dianfanbao,
            R.drawable.chouyouyanji, R.drawable.weibolu, R.drawable.xiwanji, R.drawable
            .jidinghe,R.drawable.qita,0};
    private String[] itemsName = {"电视","空调","电饭煲","抽油烟机","微波炉","洗碗机","机顶盒","其他",""};

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(),ZhilianbaoActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhilianbao);
        ButterKnife.bind(this);
        mGv.setAdapter(new ZhilianbaoItemAdapter());
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position!=8) {
                    startActivity(NothingActivity.newIntent(itemsName[position]));
                }
            }
        });
    }

    private class ZhilianbaoItemAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return itemsName.length;
        }

        @Override
        public String getItem(int position) {
            return itemsName[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null) {
                convertView = View.inflate(ZhilianbaoActivity.this,R.layout.layout_item_home_gv,null);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                holder.right = convertView.findViewById(R.id.view_right);
                holder.bottom = convertView.findViewById(R.id.view_bottom);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.right.setVisibility(View.INVISIBLE);
            if((position+1)%3==0) {
                holder.right.setVisibility(View.INVISIBLE);
            } else {
                holder.right.setVisibility(View.VISIBLE);
            }
            if(imgs[position] != 0) {
                holder.iv.setImageResource(imgs[position]);
                holder.tv.setText(itemsName[position]);
            } else {
                holder.iv.setImageDrawable(new BitmapDrawable());
                holder.tv.setText("");
            }
            return convertView;
        }
    }

    static class ViewHolder {
        public ImageView iv;
        public TextView tv;
        public View right;
        public View bottom;
    }
}
